<article id="wikiArticle">
<div>
<div></div>
</div>
<p><strong>标记语句</strong>可以和 <a href="Reference/Statements/break" title="break 语句中止当前循环，switch语句或label 语句，并把程序控制流转到紧接着被中止语句后面的语句。"><code>break</code></a> 或 <a href="Reference/Statements/continue" title="continue 语句结束当前（或标签）的循环语句的本次迭代，并继续执行循环的下一次迭代。"><code>continue</code></a> 语句一起使用。标记就是在一条语句前面加个可以引用的标识符。</p>
<div class="note">
<p>标记的循环或块非常罕见。通常可以使用函数调用而不是循环跳转。</p>
</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><em>label</em> :
   <em>statement</em>
</code></pre>
<dl>
<dt><code>label</code></dt>
<dd>任何不是保留关键字的 JavaScript 标识符。</dd>
<dt><code>statement</code></dt>
<dd>语句。<code>break</code> 可用于任何标记语句，而 <code>continue</code> 可用于循环标记语句。</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>可使用一个标签来唯一标记一个循环，然后使用 <code>break</code> 或 <code>continue</code> 语句来指示程序是否中断循环或继续执行。</p>
<p>需要注意的是 JavaScript <strong>没有</strong> <code>goto</code> 语句，标记只能和 <code>break</code> 或 <code>continue</code> 一起使用。</p>
<p>在<a href="Reference/Strict_mode">严格模式</a>中，你不能使用“<code>let</code>”作为标签名称。它会抛出一个<a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>（let是一个保留的标识符）。</p>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="for_循环中使用带标记_continue">for 循环中使用带标记 <code>continue</code></h3>
<pre><code  class="language-javascript">var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 &amp;&amp; j == 1) {
         continue loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
//   "i = 2, j = 0"
//   "i = 2, j = 1"
//   "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
</code></pre>
<h3 id="Example_2" name="Example_2">使用带标记 <code>continue</code> 语句</h3>
<p>给定一组数据和一组测试，下面的例子统计通过测试的数据。</p>
<pre><code  class="language-javascript">var itemsPassed = 0;
var i, j;

top:
for (i = 0; i &lt; items.length; i++){
  for (j = 0; j &lt; tests.length; j++){
    if (!tests[j].pass(items[i])){
      continue top;
    }
  }

  itemsPassed++;
}</code></pre>
<h3 id="for_循环中使用带标记的break">for 循环中使用带标记的<code>break</code></h3>
<pre><code  class="language-javascript">var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 &amp;&amp; j == 1) {
         break loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
// Notice the difference with the previous continue example</code></pre>
<h3 id="Example_3" name="Example_3">使用带标记 <code>break</code> 语句</h3>
<p>给定一组数据和一组测试，下面的例子判断是否所有的数据均通过了测试。</p>
<pre><code  class="language-javascript">var allPass = true;
var i, j;

top:
for (i = 0; items.length; i++)
  for (j = 0; j &lt; tests.length; i++)
    if (!tests[j].pass(items[i])){
      allPass = false;
      break top;
    }</code></pre>
<h3 id="在标记块中使用_break">在标记块中使用 <code>break</code></h3>
<p>你可以在块中使用记，但只有<code>break</code>语句可以使用非循环标记。</p>
<pre><code  class="language-javascript">foo: {
  console.log('face');
  break foo;
  console.log('this will not be executed');
}
console.log('swap');

// this will log:

// "face"
// "swap</code></pre>
<h3 id="标记函数声明">标记函数声明</h3>
<p>从ECMAScript 2015开始，标准的函数声明现在对规范的<a class="external" href="http://www.ecma-international.org/ecma-262/6.0/#sec-labelled-function-declarations" rel="noopener">Web兼容性附件</a>中的非严格代码进行了标准化。</p>
<pre><code  class="language-javascript">L: function F() {}</code></pre>
<p>在<a href="Reference/Strict_mode">严格模式</a>中，这会抛出 <a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>：</p>
<pre><code  class="language-javascript">'use strict';
L: function F() {}
// SyntaxError: functions cannot be labelled
</code></pre>
<p><a href="Reference/Statements/function*">生成器函数</a>既不能在严格模式中标记，也不能在非严格模式中标记：</p>
<pre><code  class="language-javascript">L: function* F() {}
// SyntaxError: generator functions cannot be labelled</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 3rd Edition (ECMA-262)">ECMAScript 3rd Edition (ECMA-262)</a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition. Implemented in JavaScript 1.2</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-12.12" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Labelled statement</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-labelled-statements" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Labelled statement</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-labelled-statements" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Labelled statement</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>label</code></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              1</td><td class="bc-supports-yes bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              4</td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
                  
                </abbr></span></dt><dd>Full support</dd></dl></section></div><p></p>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Statements/break" title="break 语句中止当前循环，switch语句或label 语句，并把程序控制流转到紧接着被中止语句后面的语句。"><code>break</code></a></li>
<li><a href="Reference/Statements/continue" title="continue 语句结束当前（或标签）的循环语句的本次迭代，并继续执行循环的下一次迭代。"><code>continue</code></a></li>
</ul>
</article>